package com.quark.web.ui.field;

import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.quark.data.type.QuarkPair;
import com.quark.data.type.QuarkVariable;
import com.quark.framework.ormapping.QuarkOrmBaseManager;
import com.quark.framework.ormapping.QuarkOrmEntity;
import com.quark.framework.ormapping.QuarkOrmProperty;
import com.quark.framework.utility.QuarkLog;
import com.quark.framework.utility.QuarkUtilHtmlEscape;
import com.quark.web.ui.base.QuarkUiElementBase;
import com.quark.web.ui.base.QuarkUiSelectElement;
import com.quark.web.ui.base.QuarkUiSetting;

public class QuarkUiSelectField extends QuarkUiFieldBase{

	private QuarkUiSelectElement element = null;
	public QuarkUiSelectField(String fieldName) {
		super(fieldName, new QuarkUiSelectElement(fieldName, fieldName, QuarkUiSetting.SELECT_CLASS, ""));
		element = (QuarkUiSelectElement) elementBase;
		// TODO Auto-generated constructor stub
	}
	
	public String createField()
	{
		return element.createElement();
	}
	
	public void clearField()
	{
		element.clearOption();
	}
	
	public void setCheck(String value)
	{
		element.setCheck(value);
	}
	
	public boolean fillFieldWith(Object param)
	//Accept List<QuarkPair<String ,Object>> / QuarkPair<String,Object>[]
	{
		clearField();
		if(param != null)
		{
			if(param.getClass().getSuperclass().equals(AbstractList.class))
			{
				List<?> list = (List<?>)param;
				String checkValue = null;
				for(Object i : list)
				{
					QuarkPair<?, ?> pair = (QuarkPair<?, ?>)i;
					if(pair.first.toString().equalsIgnoreCase("@check"))
					{
						checkValue = pair.second.toString();
						continue;
					}
					element.addOption(QuarkUtilHtmlEscape.escape(new QuarkVariable(pair.second, null).toString()), pair.first.toString());
				}
				if(checkValue != null)
				{
					setCheck(checkValue);
				}
			}
			else if(
					param.getClass().isArray() && 
					param.getClass().getComponentType().equals(Integer.class)
					)
			{
				Object[] array = (Object[]) param;
				String checkValue = null;
				for(Object i : array)
				{
					QuarkPair<?, ?> pair = (QuarkPair<?, ?>)i;
					if(pair.first.toString().equalsIgnoreCase("@check"))
					{
						checkValue = pair.second.toString();
						continue;
					}
					element.addOption(QuarkUtilHtmlEscape.escape(new QuarkVariable(pair.second, null).toString()), pair.first.toString());
				}
				if(checkValue != null)
				{
					setCheck(checkValue);
				}
			}
			else
			{
				QuarkLog.logError(
						getClass().getName(), 
						"can't fill with " + 
						param.getClass().getName() + 
						", " + 
						List.class.getName() + 
						"<QuarkPair<String, Object>> or QuarkPair<String, Object> expected"
						);
				return false;
			}

		}
		else
		{
			QuarkLog.logMessage(getClass().getName(), "can't fill field with null");
		}
		return true;
	}
	
	public Object processSubmitData(Object rawData)
	{
		return super.processSubmitData(rawData);
	}
	
	public QuarkUiElementBase getElement() 
	{
		return element;
	}
}
